package com.app.service.impl;

import com.app.mapper.DepartmentMapper;
import com.app.mapper.KeyWorkMapper;
import com.app.model.DepartmentModel;
import com.app.model.KeyWorkModel;
import com.app.model.ProjectModel;
import com.app.model.SystemModel;
import com.app.service.KeyWorkService;
import com.app.vo.KeyWorkVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class KeyWorkServiceImpl implements KeyWorkService {
    @Autowired
    private KeyWorkMapper keyWorkMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public KeyWorkVo keyWorkList(Integer type) {
        //部门列表
        List<DepartmentModel> modelList = departmentMapper.getDepartment();
        //部门的类别列表
        List<SystemModel> departmentClass = departmentMapper.getDepartmentClass();
        KeyWorkVo vo = new KeyWorkVo();
        if (type == 1) {
            List<DepartmentModel> list = new ArrayList<>();
            for (int i = 0, j = 0; i < modelList.size(); i++) {
                if (modelList.get(i).getType() == 18) {
                    list.add(j, modelList.get(i));
                    j++;
                }
            }
            for (int i = 0; i < modelList.size(); i++) {
                modelList.get(i).setDepartmentId(modelList.get(i).getId());
            }
            vo.setList1(list);
            vo.setCount(list.size());
            return vo;
        } else if (type == 2) {
            List<DepartmentModel> list = new ArrayList<>();
            for (int i = 0, j = 0; i < modelList.size(); i++) {
                if (modelList.get(i).getType() == 19) {
                    list.add(j, modelList.get(i));
                    j++;
                }
            }
            for (int i = 0; i < modelList.size(); i++) {
                modelList.get(i).setDepartmentId(modelList.get(i).getId());
            }
            vo.setList1(list);
            vo.setCount(list.size());
            return vo;
        } else if (type == 3) {
            List<DepartmentModel> list = new ArrayList<>();
            for (int i = 0, j = 0; i < modelList.size(); i++) {
                if (modelList.get(i).getType() == 20) {
                    list.add(j, modelList.get(i));
                    j++;
                }
            }
            for (int i = 0; i < modelList.size(); i++) {
                modelList.get(i).setDepartmentId(modelList.get(i).getId());
            }
            vo.setList1(list);
            vo.setCount(list.size());
            return vo;
        } else {
            for (int i = 0; i < modelList.size(); i++) {
                modelList.get(i).setDepartmentId(modelList.get(i).getId());
            }
            int count = departmentMapper.count();
            vo.setList1(modelList);
            vo.setCount(count);
            return vo;
        }
    }

    @Override
    public List<KeyWorkModel> getListByModel(KeyWorkModel model) {
        return keyWorkMapper.getListByModel(model);
    }

    @Override
    public int getCountByModel(KeyWorkModel model) {
        return keyWorkMapper.getCountByModel(model);
    }

    @Override
    public KeyWorkVo contentList(Integer id) {
        List<KeyWorkModel> content = getContent(id);
        int countById = getCountById(id);
        KeyWorkVo vo = new KeyWorkVo();
        vo.setList(content);
        vo.setCount(countById);
        return vo;
    }

    @Override
    public List<KeyWorkModel> getContent(Integer departmentId) {

        return keyWorkMapper.getContent(departmentId);
    }

    @Override
    public int getCountById(Integer id) {
        return keyWorkMapper.getCountById(id);
    }

    @Override
    public void set(KeyWorkModel model) {
        keyWorkMapper.updateById(model);
    }

    @Override
    public void insert(KeyWorkModel model) {
        keyWorkMapper.insert(model);
    }

    @Override
    public void deleteProjectById(int id) {
        keyWorkMapper.deleteById(id);
    }

    @Override
    public KeyWorkVo keyWork(KeyWorkModel model) {
        List<KeyWorkModel> keyWorkModels = keyWorkMapper.keyWork(model);
        Integer contentByTask = keyWorkMapper.getContentByTask(model);
        KeyWorkVo vo = new KeyWorkVo();
        vo.setCount(contentByTask);
        vo.setList(keyWorkModels);
        return vo;
    }

    @Override
    public List<KeyWorkModel> getListByDepartmentId(Integer departmentId) {

        QueryWrapper<KeyWorkModel> queryWrapper = new QueryWrapper<>();
        if (departmentId > 0) {
            queryWrapper.select().eq("department_id", departmentId);
        }
        Calendar calendar = Calendar.getInstance();
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        queryWrapper.select().eq("year", year);
        List<KeyWorkModel> keyWorkModelList = keyWorkMapper.selectList(queryWrapper);
        return keyWorkModelList;
    }

    @Override
    public KeyWorkModel getKeyWorkCount() {
        KeyWorkModel model = new KeyWorkModel();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formats = new SimpleDateFormat("yyyy");
        //获取当前年
        int thisYear = calendar.get(Calendar.YEAR);
        //获取去年年份
        calendar.add(Calendar.YEAR, -1);
        Date time = calendar.getTime();
        String lastYear = formats.format(time);
        model.setThisYearCount(keyWorkMapper.selectByYear(String.valueOf(thisYear)));
        model.setLastYearCount(keyWorkMapper.selectByYear(lastYear));
        return model;
    }


}
